package com.mission.schedule.activity;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.mission.schedule.R;
import com.mission.schedule.annotation.ViewResId;
import com.mission.schedule.applcation.App;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.NetUtil;
import com.mission.schedule.utils.ProgressUtil;
import com.mission.schedule.utils.SharedPrefUtil;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OpinionBackActivity extends BaseActivity {

    @ViewResId(id = R.id.top_ll_back)
    private LinearLayout top_ll_back;
    @ViewResId(id = R.id.headtitle_tv)
    private TextView headtitle_tv;
    @ViewResId(id = R.id.content)
    private TextView content;
    @ViewResId(id = R.id.send)
    private TextView send;

    Context context;

    @Override
    protected void setListener() {
        top_ll_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                OpinionBackActivity.this.finish();
            }
        });
        send.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                postPersonCalerder(content.getText().toString());
            }
        });
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_opinionback);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        context = this;
        headtitle_tv.setText("意见反馈");
    }

    @Override
    protected void setAdapter() {

    }

    private void postPersonCalerder(String message) {
        if (NetUtil.getConnectState(OpinionBackActivity.this) == NetUtil.NetWorkState.NONE) {
            Toast.makeText(context, "请检查你的网络情况!", Toast.LENGTH_SHORT).show();
        } else {
            if (message.length() < 8 || message.length() > 200) {
                Toast.makeText(context, "内容在8-200字以内!", Toast.LENGTH_SHORT).show();
            } else {
                SharedPrefUtil sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);

                PersonCalerder("5001", "5001", sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.USERID, "0"),
                        DateUtil.formatDateTimeSs(new Date()), sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.USERNAME, ""),
                        message
                );
            }
        }
    }

    final ProgressUtil progressUtil = new ProgressUtil();

    /**
     * 对好友日程进行操作
     *
     * @param cpid
     * @param calendaId
     * @param uid
     * @param time
     * @param name
     * @param message
     */
    private void PersonCalerder(final String cpid, final String calendaId, final String uid, final String time, final String name, final String message) {
        progressUtil.ShowProgress(context, true, true, "意见发送中...");
        final JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        try {
            //添加的json
            jsonObject.put("CAlarmsound", "g_220");
            jsonObject.put("CAlarmsoundDesc", "短提示音");
            jsonObject.put("CLightAppId", 0);
            jsonObject.put("COpenstate", 1);
            jsonObject.put("CPostpone", 1);
            jsonObject.put("CRecommendName", name);
            jsonObject.put("CTags", "");
            jsonObject.put("CType", 0);
            jsonObject.put("CTypeDesc", "");
            jsonObject.put("CTypeSpare", "");
            jsonObject.put("calendaId", 0);
            jsonObject.put("cbeforTime", 0);
            jsonObject.put("cdate", time.substring(0, 10));
            jsonObject.put("changTime", time);
            jsonObject.put("cisAlarm", 0);
            jsonObject.put("cpId", cpid);
            jsonObject.put("cretetime", time);
            jsonObject.put("ctime", time.substring(11, 16));

            jsonObject.put("atype", 0);
            jsonObject.put("downNum", 0);
            jsonObject.put("downstate", 0);
            jsonObject.put("endNum", 0);
            jsonObject.put("endState", "0");
            jsonObject.put("id", Integer.valueOf(calendaId));
            jsonObject.put("imgPath", "");
            jsonObject.put("lyNum", 0);
            jsonObject.put("message", message);
            jsonObject.put("parReamrk", "");
            jsonObject.put("pid", "");
            jsonObject.put("poststate", "0");
            jsonObject.put("remark", "");
            jsonObject.put("remark1", android.os.Build.MANUFACTURER + " " + android.os.Build.MODEL);
            jsonObject.put("remark2", "");
            jsonObject.put("remark3", "");
            jsonObject.put("repCalendaState", 0);
            jsonObject.put("repCalendaTime", "");
            jsonObject.put("repColorType", 0);
            jsonObject.put("repDisplayTime", 0);
            jsonObject.put("repInStable", 1);
            jsonObject.put("repIsPuase", 0);
            jsonObject.put("repState", 0);
            jsonObject.put("repType", 0);
            jsonObject.put("repTypeParameter", "");
            jsonObject.put("repdatetwo", "");
            jsonObject.put("repinitialcreatedtime", "");
            jsonObject.put("replastcreatedtime", "");
            jsonObject.put("repnextcreatedtime", "");
            jsonObject.put("repstartdate", "");

            jsonObject.put("repstatetwo", 0);
            jsonObject.put("schIsImportant", 0);
            jsonObject.put("status", 1);
            jsonObject.put("uid", Integer.valueOf(uid));
            jsonObject.put("webUrl", "");


        } catch (JSONException e) {
            e.printStackTrace();
        }

        jsonArray.put(jsonObject);

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URLConstants.HOST + "/appFrends_synFrendCalendaNew.do",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String result) {
                        if (!TextUtils.isEmpty(result)) {
                            try {
                                Gson gson = new Gson();
                                FriendsSouSuoActivity.PersonCalerder personCalerder = gson.fromJson(result, FriendsSouSuoActivity.PersonCalerder.class);
                                if (personCalerder.status == 0) {
                                    List<FriendsSouSuoActivity.PersonCalerderListBean> personCalerderListBeanList = personCalerder.list;
                                    if (personCalerderListBeanList.get(0).getState() == 0) {
                                        content.setText("");
                                        Toast.makeText(context, "反馈成功!", Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(context, "请检查你的网络情况!", Toast.LENGTH_SHORT).show();
                                    }
                                } else {
                                    Toast.makeText(context, "请检查你的网络情况!", Toast.LENGTH_SHORT).show();
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                                Toast.makeText(context, "请检查你的网络情况!", Toast.LENGTH_SHORT).show();
                            }
                        }
                        progressUtil.dismiss();
                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d("TAG", "error:" + volleyError.toString());
                progressUtil.dismiss();
                Toast.makeText(context, "请检查你的网络情况!", Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                //添加
                map.put("data", "{addData:" + jsonArray.toString() + ",updateData:[],deleData:[]}");

                return map;
            }
        };
        stringRequest.setTag("synFrendCalendaNew");
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(100000, 1, 1.0f));
        App.queues.add(stringRequest);
    }

}
